/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package vn.ugame.module.comment;

import com.google.gson.Gson;
import java.io.IOException;
import java.util.ResourceBundle;
import javax.servlet.ServletException;
import vn.ugame.exception.ExceptionCode;
import vn.ugame.model.bl.FindResult;
import vn.ugame.model.bl.StoreService;
import vn.ugame.model.entity.Comment;
import vn.ugame.module.AbstractModule;
import vn.ugame.module.ActionConfig;
import vn.ugame.module.Param;
import vn.ugame.utility.StringEscape;

/**
 *
 * @author user
 */
public class CommentModule extends AbstractModule implements vn.ugame.module.Module {

    public CommentModule() {
    }

    @ActionConfig(urlPattern = {"^(?<itemId>\\w+)/(?<page>\\d+)$"})
    public void displayComment(@Param("itemId") String itemId, @Param("page") int page)
            throws ServletException, IOException {
        try {
            /**
             * Set default values for parameters
             */
            page = page <= 0 ? 1 : page;
            /**
             * declare service
             */
            StoreService service = new StoreService();

            /**
             * Prepare alternative model for display item action
             */
            // Create model
            FindResult<Comment> comments = service.findComments(itemId, page);

            //Save model into json
            String json = new Gson().toJson(comments);

            getResponse().setContentType("application/json");
            getResponse().setCharacterEncoding("UTF-8");
            getResponse().getWriter().write(json);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    @ActionConfig(urlPattern = {"^create/(?<itemId>\\w+)$"})
    public void sendComment(@Param("itemId") String itemId)
            throws ServletException, IOException {
        String msg = "";
        ResourceBundle resourceBundle = ResourceBundle.getBundle("vn.ugame.bundle.Language_en_US");
        try {
            /**
             * Get parameters from request
             */
            String locale = getRequest().getParameter("locale");
            locale = locale.substring(0, 2) + "_" + locale.substring(3).toUpperCase();
            ResourceBundle rb = ResourceBundle.getBundle("vn.ugame.bundle.Language_" + locale);
            if (rb != null) {
                resourceBundle = rb;
            }
            String content = getRequest().getParameter("content");
            String email = getRequest().getSession().getAttribute("AuthPrincipal").toString();

            /*
             * Replace special charater
             */
            content = StringEscape.getInstance().escapeHtml(content);
            /**
             * declare service
             */
            StoreService service = new StoreService();

            service.createComment(itemId, email, content);
        } catch (NullPointerException npex) {
            msg = resourceBundle.getString("ugame.exception-16");
        } catch (Exception ex) {
            msg = resourceBundle.getString("ugame.exception-" + ExceptionCode.ERROR);
        }

        if (msg != null && !msg.isEmpty()) {
            String json = new Gson().toJson(msg);

            getResponse().setContentType("application/json");
            getResponse().setCharacterEncoding("UTF-8");
            getResponse().getWriter().write(json);
        }
    }
}
